package com.next.aap.server.controller;

import java.util.ArrayList;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.next.aap.client.AllItem;
import com.next.aap.client.BlogItem;
import com.next.aap.client.CandidateSelectionProcess;
import com.next.aap.client.DonationSummary;
import com.next.aap.client.EventItem;
import com.next.aap.client.NewsItem;
import com.next.aap.client.RegisterDeviceResponse;
import com.next.aap.client.RegisterFacebookUserProfile;
import com.next.aap.client.RegisterFacebookUserResponse;
import com.next.aap.client.VideoItem;
import com.next.aap.messages.NewNewsMessage;
import com.next.aap.messages.NotificationMessage;
import com.next.aap.messages.TaskStatus;
import com.next.core.exception.AppException;

@Controller
public class ApiController extends BaseController{

	
	AllItem allItem;
	private String response;
	
	public ApiController(){
		response = readFromFile();
		Gson gson = new Gson();
		if(response == null || response.trim().isEmpty()){
			allItem = new AllItem();
			allItem.setBlogItems(new ArrayList<BlogItem>());
			allItem.setEventItems(new ArrayList<EventItem>());
			allItem.setNewsItems(new ArrayList<NewsItem>());
			allItem.setVideoItems(new ArrayList<VideoItem>());
			response = gson.toJson(allItem);
		}else{
			allItem = gson.fromJson(response, AllItem.class); 
		}
		
	}
	
	
	@RequestMapping(value="/user/register/facebook", method = RequestMethod.POST)
    @ResponseBody
    public RegisterFacebookUserResponse registerFacebookUserProfile(@RequestBody RegisterFacebookUserProfile registerFacebookUserProfile){
		RegisterFacebookUserResponse registerFacebookUserResponse = new RegisterFacebookUserResponse(); 
		try {
			String deviceRegId = registerFacebookUserProfile.getDeviceRegId();
			appService.registerDevice(deviceRegId);
			registerFacebookUserResponse = appService.registerFacebookUserAsync(registerFacebookUserProfile);
		} catch (AppException e) {
			registerFacebookUserResponse.setStatus(RegisterFacebookUserResponse.STATUS_FAILED);
			logger.error("Error while registering user", e);
		}
		return registerFacebookUserResponse;
	}

	@RequestMapping(value="/get/all", method = RequestMethod.GET)
    @ResponseBody
    public String getAllItems(HttpServletResponse httpResponse){
		//httpResponse.setDateHeader("Last-Modified", System.currentTimeMillis());
		return response;
	}
	@RequestMapping(value="/csp/get/eng", method = RequestMethod.GET)
    @ResponseBody
    public String getCandidateSelectionProcessEng(HttpServletResponse httpResponse){
		String candidateSelectionProceEng = delhiElectionCache.getCandidateSelectionProceEnglish();
		return candidateSelectionProceEng;
	}
	@RequestMapping(value="/csp/get/hin", method = RequestMethod.GET)
    @ResponseBody
    public String getCandidateSelectionProcessHindi(HttpServletResponse httpResponse){
		String candidateSelectionProceHindi = delhiElectionCache.getCandidateSelectionProceHindi();
		return candidateSelectionProceHindi;
	}
	@RequestMapping(value="/csp/add/hin", method = RequestMethod.POST)
    @ResponseBody
    public String addHindiCandidateSelectionProcess(@RequestBody CandidateSelectionProcess candidateSelectionProcess){
		String json = new Gson().toJson(candidateSelectionProcess);
		delhiElectionCache.setCandidateSelectionProceHindi(json);
		return json;
	}
	@RequestMapping(value="/csp/add/eng", method = RequestMethod.POST)
    @ResponseBody
    public String addEnglishCandidateSelectionProcess(@RequestBody CandidateSelectionProcess candidateSelectionProcess){
		String json = new Gson().toJson(candidateSelectionProcess);
		delhiElectionCache.setCandidateSelectionProceEnglish(json);
		return json;
	}
	@RequestMapping(value="/news/clear", method = RequestMethod.GET)
    @ResponseBody
    public String clearAllNews(){
		allItem.getNewsItems().clear();
		response = convertToJson(allItem); 
		return response;
	}
	@RequestMapping(value="/news/clear/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String clearParticularNews(@PathVariable long id){
		int index = -1;
		int i=0;
		for(NewsItem oneNewsItem:allItem.getNewsItems()){
			if(oneNewsItem.getId().equals(id)){
				index = i;
				break;
			}
			i++;
		}
		if(index >= 0 ){
			allItem.getNewsItems().remove(index);
			response = convertToJson(allItem); 
			return response;
		}
		return response;
		
		
	}
	@RequestMapping(value="/news/add", method = RequestMethod.POST)
    @ResponseBody
    public String addNews(@RequestBody NewsItem newsItem){
		allItem.getNewsItems().add(0,newsItem);
		while(allItem.getNewsItems().size() > 10){
			allItem.getNewsItems().remove(10);
		}
		NewNewsMessage newNewsMessage = new NewNewsMessage();
		newNewsMessage.setNotificationTitle(newsItem.getTitle());
		newNewsMessage.setTotalItem(1);
		taskManager.runSendMessageTask(NotificationMessage.NEW_NEWS_MESSAGE, newNewsMessage);
		response = convertToJson(allItem); 
		return response;
	}
	@RequestMapping(value="/video/clear", method = RequestMethod.GET)
    @ResponseBody
    public String clearAllVideos(){
		allItem.getVideoItems().clear();
		response = convertToJson(allItem); 
		return response;
	}
	@RequestMapping(value="/video/add", method = RequestMethod.POST)
    @ResponseBody
    public String addVideo(@RequestBody VideoItem newsItem){
		allItem.getVideoItems().add(0,newsItem);
		while(allItem.getVideoItems().size() > 10){
			allItem.getVideoItems().remove(10);
		}
		NewNewsMessage newNewsMessage = new NewNewsMessage();
		newNewsMessage.setNotificationTitle(newsItem.getTitle());
		newNewsMessage.setTotalItem(1);
		taskManager.runSendMessageTask(NotificationMessage.NEW_VIDEO_MESSAGE, newNewsMessage);
		response = convertToJson(allItem); 
		return response;
	}
	@RequestMapping(value="/blog/add", method = RequestMethod.POST)
    @ResponseBody
    public String addBlog(@RequestBody BlogItem newsItem){
		allItem.getBlogItems().add(0,newsItem);
		while(allItem.getBlogItems().size() > 10){
			allItem.getBlogItems().remove(10);
		}
		response = convertToJson(allItem); 
		return response;
	}
	@RequestMapping(value="/event/add", method = RequestMethod.POST)
    @ResponseBody
    public String addEvent(@RequestBody EventItem newsItem){
		allItem.getEventItems().add(0,newsItem);
		while(allItem.getEventItems().size() > 10){
			allItem.getEventItems().remove(10);
		}
		response = convertToJson(allItem); 
		return response;
	}
	@RequestMapping(value="/device/register/android/{deviceId}", method = RequestMethod.GET)
    @ResponseBody
    public RegisterDeviceResponse registerDevice(@PathVariable String deviceId){
		//deviceList.add(deviceId);
		RegisterDeviceResponse registerDeviceResponse;
		try {
			registerDeviceResponse = appService.registerDeviceAsync(deviceId);
		} catch (AppException e) {
			registerDeviceResponse = new RegisterDeviceResponse();
			registerDeviceResponse.setStatus(TaskStatus.STATUS_FAILED);
			e.printStackTrace();
		}
		return registerDeviceResponse;
	}
	@RequestMapping(value="/device/show", method = RequestMethod.GET)
    @ResponseBody
    public String showDevices(){
		return "";//appService.showDevices();
	}
	@RequestMapping(value="/send", method = RequestMethod.GET)
    @ResponseBody
    public String sendMessage(@RequestParam String msg){
		//appService.sendMessage(msg);
		return "Message Sent";
	}
	
	@RequestMapping(value="/facebook/getfeed", method = RequestMethod.GET)
    @ResponseBody
    public String getFacebookFeed(){
		return facebookCache.getFacebookFeed();
	}
	
	@RequestMapping(value="/donation/update", method = RequestMethod.POST)
    @ResponseBody
    public String updateDonationDetail(@RequestBody DonationSummary donationSummary){
		String json = new Gson().toJson(donationSummary);
		donationCache.setDonationSummary(json);
		return json;
	}
	
	@RequestMapping(value="/donation/get", method = RequestMethod.GET)
    @ResponseBody
    public String getDonationSummary(){
		return donationCache.getDonationSummary();
	}
	
}
